import {BaseService} from 'egg-cool-service';

/**
 * 商品-种草
 */
export default class GoodsRecommendService extends BaseService {

    /**
     * 根据ID获得种草信息
     * @param id
     * @param entity
     */
    public async info (id , entity) {
        const info = await entity.findOne ({id});
        info.id = Number (id);
        info.productId = Number (info.productId);
        return info;
    }

    /**
     * 分页查询
     * @param query
     */
    async page (query) {
        const {} = query;
        // d.name AS cateName

        const sql = `
        SELECT
            a.id,a.createTime,a.updateTime,a.productId,a.height,a.type,a.orderNum,
            d.bannerImage AS bannerImage,d.summary AS summary,d.name AS productName,d.files AS files
        FROM
            goods_recommend a
            LEFT JOIN goods_product d on a.productId = d.id
        WHERE 1 = 1
        GROUP BY a.id
        `;
        // ORDER BY a.orderNum ASC
        // GROUP BY a.id
        // console.log(sql)
        // GROUP BY a.id
        // console.log(sql)
        const result = await this.sqlRenderPage (sql , query);
        if ( result.list ) {
            result.list.map (item => {
                item.id = Number (item.id);
                item.files = item.files ? item.files.split (this.app.config.charSPLIT).map (data => {
                    let option = {url : ''};
                    option.url = data;
                    return option;
                }) : [];
                return item;
            });
        }
        return result;
    }

}
